﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Core.Utility.Helper
{
    /// <summary>
    /// CultureInfo 助手类
    /// </summary>
    public class CultureInfoHelper
    {
        /// <summary>
        /// 获取所有的 <see cref="CultureInfo"/> 列表
        /// 注意，此列表中并无 zh-CN 而是返回 zh-Hans-CN
        /// </summary>
        /// <returns></returns>
        public static List<CultureInfo> AllCultureInfoList
        {
            get
            {
                return CultureInfo.GetCultures(CultureTypes.AllCultures | CultureTypes.NeutralCultures).ToList();
            }
        }


        /// <summary>
        /// 获取区域名称，通过 传入 name， 通过 <see cref="AllCultureInfoList"/> 获取 对应的 <see cref="CultureInfo"/>
        /// <para> 若从<see cref="AllCultureInfoList"/> 中获取失败，将尝试 new <see cref="CultureInfo"/>(name) </para>
        /// </summary>
        /// <param name="name">区域名称</param>
        /// <param name="defaultValue">获取失败返回的默认值</param>
        /// <param name="isFindZhHans">是否将name转换为zh-Hand-xx的形式查找zh-Hans</param>
        /// <param name="ignoreCase">true:大小写不敏感</param>
        /// <returns>若值不存在，返回指定的默认值</returns>
        public static CultureInfo? GetCultureInfo(string? name, CultureInfo? defaultValue = null,bool isFindZhHans = false, bool ignoreCase = true)
        {
            CultureInfo? res = defaultValue;
            if (string.IsNullOrEmpty(name))
            {
                return res;
            }
            //由于AllCultureInfoList返回的列表中并无 zh-CN 而是 zh-Hans-CN,为兼容，添加hans-Name变量
            string hansName = name;
            string souraceName = name;
            if (isFindZhHans)
            {
                if (name.StartsWith("zh-") && name.ToLower().Contains("Hans".ToLower()) == false)
                {
                    List<string> strSplit = name.Split("-").ToList();
                    if (strSplit.Count > 0)
                    {
                        strSplit.Insert(1, "Hans");
                    }
                    hansName = string.Join("-", strSplit);
                }
            }

            CultureInfo? findValue = null;
            if (ignoreCase)
            {
                name = name.ToLower();
                findValue = AllCultureInfoList.FirstOrDefault(p => p.Name.ToLower() == name || p.Name.ToLower() == hansName.ToLower());
            }
            else
            {
                findValue = AllCultureInfoList.FirstOrDefault(p => p.Name == name || p.Name == hansName);
            }
            if (findValue != null)
            {
                res = findValue;
            }
            else
            {
                try
                {
                    findValue = new CultureInfo(souraceName);
                    if (findValue != null)
                    {
                        res = findValue;
                    }
                }
                catch
                {

                }
            }
            return res;
        }

        /// <summary>
        /// 获取区域名称，通过 传入 name， 通过 <see cref="AllCultureInfoList"/> 获取 对应的 <see cref="CultureInfo"/>
        /// </summary>
        /// <param name="name">区域名称</param>
        /// <param name="defaultValue">获取失败返回的默认值</param>
        /// <param name="ignoreCase">true:大小写不敏感</param>
        /// <returns>若值不存在，返回指定的默认值</returns>
        public static string? GetCultureInfoName(string? name,string? defaultValue = null,bool ignoreCase = true)
        {
            string? res = defaultValue;
            CultureInfo? cultureInfo = GetCultureInfo(name, null, ignoreCase);            
            if (cultureInfo != null)
            {
                res = cultureInfo.Name;
            }
            return res;
        }
    }
}
